package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ImUser;
import com.ruoyi.system.domain.dto.ImUserMessageListDto;
import com.ruoyi.system.domain.vo.ImUserMessageListVo;
import com.ruoyi.system.im.IMUserMessageTask;
import com.ruoyi.system.im.event.UserMessageEvent;
import com.ruoyi.system.mapper.ImUserMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ImUserMessageMapper;
import com.ruoyi.system.domain.ImUserMessage;
import com.ruoyi.system.service.IImUserMessageService;

import static com.ruoyi.common.utils.PageUtils.getDataTable;
import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 单聊消息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-01
 */
@Service
public class ImUserMessageServiceImpl extends ServiceImpl<ImUserMessageMapper, ImUserMessage> implements IImUserMessageService
{
    @Autowired
    private ImUserMessageMapper imUserMessageMapper;

    @Autowired
    private ImUserMapper imUserMapper;

    @Autowired
    private IMUserMessageTask imUserMessageTask;

    @Autowired
    private ApplicationContext applicationContext;


    @Override
    public TableDataInfo msgList(ImUserMessageListDto listDto) {
        //异步调用查询聊天记录
        UserMessageEvent event=new UserMessageEvent(this);
        applicationContext.publishEvent(event);

        startPage();
        LambdaQueryWrapper<ImUserMessage> imUserMessageLambdaQueryWrapper=new LambdaQueryWrapper<>();
        imUserMessageLambdaQueryWrapper.orderByDesc(ImUserMessage::getMessageTime);
        imUserMessageLambdaQueryWrapper.like(ObjectUtils.isNotEmpty(listDto.getFromUserId()),ImUserMessage::getFromUserId,listDto.getFromUserId());
        imUserMessageLambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(listDto.getToUserId()),ImUserMessage::getToUserId,listDto.getToUserId());
        imUserMessageLambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(listDto.getMessageType()),ImUserMessage::getMessageType,listDto.getMessageType());
        imUserMessageLambdaQueryWrapper.eq(ImUserMessage::getIsDelete,0);
        List<ImUserMessage> imUserMessages = imUserMessageMapper.selectList(imUserMessageLambdaQueryWrapper);

        long total = new PageInfo<>(imUserMessages).getTotal();

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ImUserMessageListVo> listVos=imUserMessages.stream().map(x->{
            ImUserMessageListVo listVo = BeanUtil.toBean(x, ImUserMessageListVo.class);
            LambdaQueryWrapper<ImUser> imUserLambdaQueryWrapper=new LambdaQueryWrapper<>();
            imUserLambdaQueryWrapper.eq(ImUser::getUserId,x.getFromUserId());
            ImUser imUser = imUserMapper.selectOne(imUserLambdaQueryWrapper);
            listVo.setFromUserNickName(imUser.getNickName());

            LambdaQueryWrapper<ImUser> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(ImUser::getUserId,x.getToUserId());
            ImUser user = imUserMapper.selectOne(userLambdaQueryWrapper);
            listVo.setToUserNickName(user.getNickName());
            long dateLo= x.getMessageTime()*1000L;
            System.out.println(dateLo);
            Date date=new Date(dateLo);
            System.out.println(date);
            String msgTime = simpleDateFormat.format(date);
            System.out.println(msgTime);
            listVo.setSendTime(msgTime);
            return listVo;
        }).collect(Collectors.toList());

        return getDataTable(listVos,total);
    }

    @Override
    public void delete(Long id) {
        ImUserMessage imUserMessage = imUserMessageMapper.selectById(id);
        imUserMessage.setIsDelete(1);
        imUserMessageMapper.updateById(imUserMessage);

    }
}
